﻿using UnityEngine;
using System.Collections;

namespace AutoRoot
{
   public class DropdownMenuController_move_camera : MonoBehaviour
   {
      public GameObject dropDownMenuKnob;
      public GameObject dropDownMenuList;

      private DropdownMenuKnob knobContrl;

      private float maxDisplacement = 0f;
      private float displacement = 0f;

      private static float keySlope = 150.0f; // Magic number
      private float currentSlope = 0.0f;

      private Vector3 updatePos = new Vector3(0, 0, 0);
      private bool slide = true;

      void Start()
      {
         knobContrl = dropDownMenuKnob.GetComponent<DropdownMenuKnob>();
         maxDisplacement = Mathf.Abs((dropDownMenuList.renderer.bounds.max.y - dropDownMenuList.renderer.bounds.min.y) - (dropDownMenuKnob.renderer.bounds.max.y - dropDownMenuKnob.renderer.bounds.min.y));
      }

      void Update()
      {
         if (!slide)
         {
            //Do not update when user slide it
            //Update when user use gesture: fast slide down & fast slide up
            Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position, updatePos, Time.deltaTime * 3);
         }
      }

      void OnTouchStay(Vector3 hitPoint)
      {
         if (knobContrl.listActivated)
         {
            slide = true;
            Vector3 newPos = new Vector3(0, hitPoint.y, 0);
            newPos = Camera.main.ScreenToWorldPoint(newPos);
            newPos.x = Camera.main.transform.position.x;
            newPos.y += (dropDownMenuKnob.renderer.bounds.max.y - dropDownMenuKnob.renderer.bounds.min.y) / 2;
            newPos.y -= (dropDownMenuList.renderer.bounds.max.y - dropDownMenuList.renderer.bounds.min.y) / 2;
            newPos.y = Vector3.Distance(Camera.main.transform.position, newPos);

            displacement += (Camera.main.transform.position.y > newPos.y ? Vector3.Distance(Camera.main.transform.position, newPos) : -Vector3.Distance(Camera.main.transform.position, newPos));

            if (newPos.y >= maxDisplacement)
            {
               newPos.y = maxDisplacement;
            }
            newPos.z = Camera.main.transform.position.z;
            Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position, newPos, Time.deltaTime * 10);
         }
      }

      void OnTouchUp(Vector3 hitPoint)
      {
         OnExit(hitPoint);
      }

      void OnTouchExit(Vector3 hitPoint)
      {
         OnExit(hitPoint);
      }

      void OnExit(Vector3 hitPoint)
      {
         if (knobContrl.listActivated)
         {
            knobContrl.exitTime = Time.time;
            knobContrl.exitY = hitPoint.y;

            currentSlope = (knobContrl.exitY - knobContrl.enterY) / (knobContrl.exitTime - knobContrl.enterTime);

            if (currentSlope < 0)
            {
               //Slide down
               if (Mathf.Abs(currentSlope) > keySlope || hitPoint.y < Screen.height / 2)
               {
                  Open();
               }
               else
               {
                  Close();
               }
            }
            else
            {
               //Slide up
               if (currentSlope > keySlope || hitPoint.y > Screen.height / 2)
               {
                  Close();
               }
               else
               {
                  Open();
               }
            }

            knobContrl.listActivated = false;
         }
      }

      void Open()
      {
         knobContrl.listActivated = true;
         updatePos.x = Camera.main.transform.position.x;
         updatePos.y = maxDisplacement;
         displacement = maxDisplacement;
         updatePos.z = Camera.main.transform.position.z;
         slide = false;
      }

      void Close()
      {
         knobContrl.listActivated = false;
         updatePos.x = Camera.main.transform.position.x;
         updatePos.y = 0;
         displacement = 0;
         updatePos.z = Camera.main.transform.position.z;
         slide = false;
      }
   }
}